package com.example.springboot_demo.proxy;

import com.example.springboot_demo.service.ProductService;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * *
 *
 * @author wb
 * @date 2025-11-08 11:41*@since 1.0.0
 */
public class CglibExample {
    private static final Map<Class<?>, Object> PROXY_CACHE = new HashMap<>();

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(ProductService.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("Before method execution");
                Object result = proxy.invokeSuper(obj, args);
                System.out.println("After method execution");
                return result;
            }
        });

        ProductService proxyInstance = (ProductService) enhancer.create();
        proxyInstance.getProduct("123");
        ProductService proxyInstance2 = (ProductService) enhancer.create();
        proxyInstance2.getProduct("123");
        System.out.println(proxyInstance+"====="+proxyInstance2);
    }
}
